#region
/* Adoor.NET - Object/Relational mapping framework leveraging ADO.NET`s powerful disconnected mode.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
#endregion

using System.Collections.Generic;
using System;
using Adoor.Expression.AST;

namespace Adoor.Expression
{
    /// <summary>
    /// Copy all entries while visiting them
    /// </summary>
    public class ExpressionCloner: IExpressionVisitor
	{
		protected ExpressionNode result;
		public virtual void VisitEscape(ExpressionEscape e)
		{
			this.result = new ExpressionEscape(e.Content);
		}

		public virtual void VisitBinary(ExpressionBinary b)
		{
			b.Exp1.Accept(this);
			ExpressionNode exp1 = this.result;
			b.Exp2.Accept(this);
			this.result = new ExpressionBinary(b.Op, exp1, this.result);
		}

		public virtual void VisitFunction(ExpressionFunction f)
		{
			ExpressionNode[] parameters = new ExpressionNode[f.Parameters.Length];
			for (int i = 0; i < parameters.Length; i++)
			{
				f.Parameters[i].Accept(this);
				parameters[i] = this.result;
			}
			this.result = new ExpressionFunction(f.Name, parameters);
		}

		public virtual void VisitIn(ExpressionIn j)
		{
			ExpressionNode[] nodes = new ExpressionNode[j.Nodes.Length];
			for (int i = 0; i < nodes.Length; i++)
			{
				j.Nodes[i].Accept(this);
				nodes[i] = this.result;
			}

			j.Exp.Accept(this);

			this.result = new ExpressionIn(this.result, nodes);
		}

		public virtual void VisitInSubQuery(ExpressionInSubquery j)
		{
			j.Exp.Accept(this);
			
			this.result = new ExpressionInSubquery(this.result, j.SubQuery);
		}

		public virtual void VisitExistsSubQuery(ExpressionExistsSubquery j)
		{
			this.result = new ExpressionExistsSubquery(j.SubQuery);
		}

		public virtual void VisitIs(ExpressionIs i)
		{
			i.Exp.Accept(this);
			this.result = new ExpressionIs(this.result, i.IsNot, i.What);
		}

		public virtual void VisitLike(ExpressionLike l)
		{
			l.Exp1.Accept(this);
			ExpressionNode exp1 = this.result;
			l.Exp2.Accept(this);
			this.result = new ExpressionLike(exp1, l.IsNotLike, this.result);
		}

		public virtual void VisitName(ExpressionName n)
		{
			this.result = new ExpressionName(n.Qualifier, n.Value);
		}

		public virtual void VisitParen(ExpressionParen p)
		{
			p.Exp.Accept(this);
			this.result = new ExpressionParen(this.result);
		}

		public virtual void VisitPrimary(ExpressionPrimary p)
		{
			this.result = new ExpressionPrimary(p.Type, p.Value);
		}

		public virtual void VisitUnary(ExpressionUnary u)
		{
			u.Exp.Accept(this);
			this.result = new ExpressionUnary(u.Op, this.result);
		}

		public virtual void VisitCorrelationFilteredNode(ExpressionCorrelationFilteredNode c)
		{
			Dictionary<string, ExpressionNode> newDic = new Dictionary<string,ExpressionNode>(StringComparer.CurrentCultureIgnoreCase);
            foreach (KeyValuePair<string, ExpressionNode> entry in c.Nodes)
            {
                ExpressionNode en = entry.Value;
                en.Accept(this);
                newDic.Add(entry.Key, this.result);
            }

			c.DefaultNode.Accept(this);
			this.result = new ExpressionCorrelationFilteredNode(new ExpressionName(c.Qualifier.Qualifier, c.Qualifier.Value), newDic, this.result);
		}
	}
}
